#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
'''

import sys

sys.path.append('/mnt/moonbrook/Moonbrook')
from aes import contact_aes, name_aes
import os
import re
import json
import time
import requests
from copy import deepcopy
from datetime import datetime, timedelta
from apscheduler.schedulers.blocking import BlockingScheduler

from base import ExportAndTjBase
from models import MongoQueryConfig, RobotConfig
from utils import Utils
from robot import DingtalkRobot

reload(sys)
sys.setdefaultencoding('utf-8')


class ExportAndTj(ExportAndTjBase):
    week_min, week_max = Utils.get_date_range(ndays=1,end_time="2022-01-20")
    def __init__(self, *args, **kwargs):
        super(ExportAndTj, self).__init__(*args, **kwargs)
        # todo other col
        self.silk_icp_col = self.silk_db.get_collection('icp')
        self.silk_ent_col = self.silk_db.get_collection('enterprise')
        self.init_file_v1(self.file_name)

    def init_file_v1(self, abs_file_name, mod='w'):
        self.write_dc_md_f = open('{}_md_dc'.format(abs_file_name), mod)  # 导出到文件
        # self.write_domain_md_f = open('{}_domain_md_dc'.format(abs_file_name), mod)  # 导出到文件

    def close_file_v1(self):
        self.write_dc_md_f.close()
        # self.write_domain_md_f.close()


    def handle_one_mongo_data(self, record_dic, save_result_dic=None):
        # 解密，提取字段数据
        nameId = record_dic.get('nameId') or ''
        icpList = record_dic.get('icpList') or []
        crawl_time_dic = record_dic.get('crawl_time') or {}
        must_exists_values = [nameId,icpList]  # 这些字段都必须是非空值
        must_has_one_exists_values = []  # 这些字段必须最少有一个是非空值
        if not self.is_data_legal(must_exists_values, must_has_one_exists_values):
            return save_result_dic

        save_result_dic['create_every_day'] += 1
        if crawl_time_dic.get('1333'):
            save_result_dic['scheduler_md_one_day_count'] += 1

            domain_set = set()
            for dic in icpList:
                domain = dic.get('domain') or ''
                if domain:
                    domain_set.add(domain)
            save_result_dic['scheduler_domain_one_day_count'] += len(domain_set)

            if domain_set:
                string = '\t'.join(list(domain_set))
                contact_ls = self.contact_col.find(
                    {"nameId": nameId, 'import_update_time': {"$gte": self.week_min}},['contactPageLink','contactSource','contactChannelList']
                )
                contact_ls = list(contact_ls)
                all_contactSource_ls = [] # 这个nameId的所有联系方式的所有来源
                all_contactPageLink_ls = [] #
                if contact_ls:
                    save_result_dic['into_contact_namaId_count'] += 1
                    for dic in contact_ls:
                        dic = Utils.decrypt_one_record(dic, need_strf_time=True, need_decrypt=True)
                        contactSource = dic.get('contactSource') or ''
                        contactPageLink = dic.get('contactPageLink') or ""
                        contactChannelList = dic.get('contactChannelList') or []
                        if contactChannelList:
                            contactSource_ls = [i.get('contactSource') for i in contactChannelList]
                        else:
                            contactSource_ls = []
                        contactSource_ls.append(contactSource)
                        contactPageLink_ls = [i.get('contactPageLink') for i in contactChannelList]
                        contactPageLink_ls.append(contactPageLink)
                        all_contactSource_ls.extend(contactSource_ls)
                        all_contactPageLink_ls.extend(contactPageLink_ls)
                        if "tungee_homepage_v3" in contactSource_ls:
                            save_result_dic['into_contact_count'] += 1

                    # if "tungee_homepage_v3" in all_contactSource_ls:
                    #     save_result_dic['into_contact_count'] += 1
                    #     self.write_dc_f.write('{}\n'.format(json.dumps(dic)))
                        # self.write_dc_f.write('{}\n'.format(json.dumps(Utils.strf_datatime(dic))))

                    for domain in domain_set:
                        is_first = True
                        for url in all_contactPageLink_ls:
                            if domain in url:
                                if is_first:
                                    save_result_dic['into_contact_domain_count'] += 1
                                    is_first = False
                                all_contactPageLink_ls.remove(url)
                        # if is_first:
                        #     self.write_domain_md_f.write('{}\t{}\n'.format(nameId, string))

                else:
                    # 名单推送了，为什么没爬到，是真没有还是啥原因
                    self.write_dc_md_f.write('{}\t探迹\t{}\n'.format(nameId, string))
                    save_result_dic['not_hit_md_count'] += 1
                    save_result_dic['not_hit_domain_count'] += len(domain_set)

        # else:
        #     self.write_dc_f.write("{}\n".format(nameId))


        return save_result_dic

    def run(self):
        # 需要统计的维度
        save_result_dic = {
            'create_every_day':0, # 每日新增icp数量
            'scheduler_md_one_day_count':0, # 每日调度名单数
            'scheduler_domain_one_day_count':0, # 每日调度的domain数
            'into_contact_namaId_count': 0,  # 入联系方式表nameId数量
            'into_contact_domain_count': 0,  # 入联系方式表名单数量
            'into_contact_count': 0,  # 入联系方式表文档数量
            'not_hit_md_count':0,#未入联系方式的名单数
            'not_hit_domain_count':0,#未入联系方式的名单数
        }
        config = {
            MongoQueryConfig.col_name: 'icp',
            MongoQueryConfig.filter: {
                "nameId": {"$exists": True},
                'icpList.domain': {"$exists": True},
                "icp_failed_no": {"$lt": 3},
                "create_time": {"$lte": self.week_max, "$gte": self.week_min},
            },
            MongoQueryConfig.projection: ['nameId',"icpList","crawl_time"],
        }
        result_dic = self.get_data_from_mongo(config, save_result_dic,is_silk=True)
        self.close_file_v1()


def run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    export_and_tj = ExportAndTj(abs_file_name)
    export_and_tj.main()


def scheduler_run(file_name):
    # py文件名/file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)
    scheduler = BlockingScheduler()
    scheduler.add_job(func=run, trigger='cron',
                      args=(abs_file_name,),
                      # day_of_week="sun",
                      hour='20', minute='52', id='dd_run')
    scheduler.start()


if __name__ == '__main__':
    file_name = '官网统计'
    run(file_name)
    # scheduler_run(file_name)
